/**
 * Created by admin on 2018/11/17.
 */

const { Builder, By, chrome,Key, until } = require('selenium-webdriver');
const {Options} = require('selenium-webdriver/chrome');


const userName = '281471259@qq.com';
const passwrod = 'TD123123td';
const loginUrl = 'https://login.tongdun.cn/login.htm';
//http://101.201.71.182:20099/tongdun/getListInfo?operationType=queryReportList&curPage=1&pageSize=10&begin_time=2018-11-11%2000:00:00&end_time=2018-11-17%2000:00:00&invoke_mode=&decision=&search_cat=report_id&search_value=&rule_name=&group=&min_score=0&max_score=100
const queryUrl='https://credit.tongdun.cn/preloanentrance/creditapply/reporthistory/preLoanReportHistory.htm';
//http://101.201.71.182:20099/tongdun/getReportInfo?report_id=ER20181211181040DAB2F19F
const reportUrl='https://credit.tongdun.cn/preloanentrance/creditapply/reporthistory/preLoanReport.htm';
const chrome_options = new Options().addArguments(['--headless','--disable-gpu','--no-sandbox']) ;



/**
 * 获取查询列表信息
 * @param ctx
 * @returns {Promise.<void>}
 */
exports.getListInfo = async (ctx) => {

  const driver = await new Builder().forBrowser('chrome')
    .setChromeOptions(chrome_options)
    .build();
  try {
    await driver.get(loginUrl);
    await driver.findElement(By.id('loginId')).sendKeys(userName);
    await driver.findElement(By.id('password')).sendKeys(passwrod);
    await driver.findElement(By.id('login_btn')).click();
    await driver.get(queryUrl+`?${ctx.originalUrl.split('?')[1]}`);

    const tongdunData = {};
    //获取当前页码信息
    await driver.findElement(By.id('hidePageSize')).getAttribute('value').then((b) => { tongdunData.pageSize = b; });
    await driver.findElement(By.id('totalPage')).getAttribute('value').then((b) => { tongdunData.totalPage = b; });
    await driver.findElement(By.id('curPage')).getAttribute('value').then((b) => { tongdunData.curPage = b; });
    // driver.getPageSource().then((result) => {
    //   // 获取页面源码
    //   console.log(result);
    // });
    const el = await driver.findElement(By.className('report-table'));
    const elTbody = await el.findElement(By.tagName('tbody'));
    const elRows = await elTbody.findElements(By.tagName('tr'));
    const listInfo = [];
    for (let i = 0; i < elRows.length; i++) {
      var elTds= await  elRows[i].findElements(By.tagName('td'));
      const rowData={};
      await  elTds[0].findElement(By.tagName('input')).then((elInput)=>{
        elInput.getAttribute('value').then((b) => { rowData.value = b; });
        elInput.getAttribute('data-sid').then((b) => { rowData.dataSid = b; });
        elInput.getAttribute('data-name').then((b) => { rowData.dataName = b; });
        elInput.getAttribute('data-pid').then((b) => { rowData.dataPid = b; });
        elInput.getAttribute('data-mobile').then((b) => { rowData.dataMobile = b; });
        elInput.getAttribute('data-loandate').then((b) => { rowData.dataLoandate = b.replace(/-/g,''); });
        elInput.getAttribute('data-loanterm').then((b) => { rowData.dataLoanterm = b; });
        elInput.getAttribute('data-loanamount').then((b) => { rowData.dataLoanamount = b; });
        elInput.getAttribute('data-loantermunit').then((b) => { rowData.dataLoantermunit = b; });
      });
      //扫描时间
      await  elTds[1].getText().then((b) => { rowData.dateTime = b; });
      //查询方式
      await elTds[4].getText().then((b) => { rowData.queryType = b; });
      await elTds[5].getText().then((b) => { rowData.appName = b; });
      await elTds[6].getText().then((b) => { rowData.login = b; });
      await elTds[7].getText().then((b) => { rowData.score = b; });
      listInfo.push(rowData);
    }
    tongdunData.listInfo = listInfo;
    ctx.body = tongdunData;
  } finally {
    await driver.quit();
  }
};

/**
 * 获取报表详细信息
 * @param ctx
 * @returns {Promise.<void>}
 */
exports.getReportInfo = async (ctx) => {
  const driver = await new Builder().forBrowser('chrome')
    .setChromeOptions(chrome_options)
    .build();
  try {
    await driver.get(loginUrl);
    await driver.findElement(By.id('loginId')).sendKeys(userName);
    await driver.findElement(By.id('password')).sendKeys(passwrod);
    await driver.findElement(By.id('login_btn')).click();
    await driver.get(reportUrl+`?${ctx.originalUrl.split('?')[1]}`);
    // console.log(await driver.getPageSource());
    const reportData = {};
    await driver.findElement(By.className("header")).getAttribute('innerHTML').then((b) => { reportData.header = b; });
    await driver.findElement(By.className("inner")).getAttribute('innerHTML').then((b) => { reportData.inner = b; });
    ctx.body = reportData;
  } finally {
    await driver.quit();
  }
};
